<?php

namespace App\Http\Controllers\admin;

use DB;
use Hash;
use Cookie;
use Illuminate\Http\Request;
use App\Models\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\controllers\common\YunpianController;
use App\Http\Controllers\common\EmailController;
class LoginController extends Controller
{

    // 登录页面
    public function getIndex()
    {
        $data = DB::table('agent')->get();
        return view('admin.login.index',['data'=>$data]);
    }
    
    // 执行注册
    public function postCreate(Request $request)
    {
        $getform = $request->except('_token','rpwd');
        $getform['pwd'] = Hash::make($getform['pwd']);
        if ($getform['agent_id'] == '0') {
            $agent['pid'] = $getform['agent_id'];
            unset($getform['agent_id']);
            $agent['name'] = $getform['name'];
            unset($getform['name']);
            $agent['path'] = '0';
            $getform['agent_id'] = DB::table('agent')->insertGetId($agent);
            if ($getform['agent_id']) {
                $getform['role_id'] = 1;
                $getform['status'] = 1;
                $getform['name'] = $getform['linkman'];
                unset($getform['linkman']);
                $row = DB::table('admin') -> insert($getform);
                if ($row) {
                    return view('admin.login.success');
                } else {
                    return back()->withInput();
                }
            }
        } else {
            $bool = DB::table('approve') -> where('account',$getform['account'])->orWhere('phone')->first();
            if(empty($bool)){
                $row = DB::table('approve') -> insert($getform);
                if ($row) {
                    return view('admin.login.success');
                }
            }
            return back()->withInput();  
        }
        
    }

    // 执行登录
    public function postLogon(Request $request)
    {
        $getdata = $request->except('_token');
        $count = 'account';
        $pattern = '/^1[3,4,5,6,7,8,9]\d{9}$/';
        $bool = preg_match($pattern,$getdata['account']);
        if ($bool) {
            $count = 'phone';
        }
        $admin = Admin::where($count,$getdata['account'])->first();
        if ($admin && Hash::check($getdata['pwd'], $admin->pwd)) {
            $data =$admin->toArray();
            $data['agent_name'] = $admin->agent->name;
            Cookie::queue('logon', $data, 0);
            return redirect('/admin/terrace/index');
        }else{
            return back()->withInput()->with('msg','登录失败！');
        }
    }

    // 跳转修改密码
    public function postForget(Request $request)
    {
        $phone = $request->input('mobile');
        $userid = DB::table('admin')->where('phone',$phone)->value('id');
        if ($userid) {
            return view('admin.login.alter',['userid'=>base64_encode($userid)]);
        } else {
            return back()->withInput()->with('msg','手机号错误！');
        }
    }

    // 短信发送验证码
    public function postCode(Request $request)
    {
        //接收前段发送的手机号
        $phone = $request->input('mobile');
        //定义验证码规则
        $codes = '0123456789';
        $num = '';
        for ($i=0;$i<4;$i++)
        {
            $num = $num.substr(str_shuffle($codes),0,1);
        }
        if ($phone && $num) {
            $text = "您的验证码是".$num;
            //调用短信平台SDK发送短信
            $yunpian = new YunpianController();
            $msg = $yunpian->Msgsend($phone, $text);
            return $num;
        }else{
            return '';
        }
    }

    // 执行修改密码
    public function postAlter(Request $request)
    {
        $id = base64_decode($request->input('user'));
        $pwd = Hash::make($request->input('pwd'));
        $row = DB::table('admin')->where('id',$id)->update(['pwd'=>$pwd]);
        if ($row) {
            return redirect('/admin/login/index');
        } else {
            return back()->withInput()->with('msg', '修改失败！');
        }
    }

    // 退出登录
    public  function getQuit()
    {
        Cookie::queue('logon', null , -1);
        $data = DB::table('agent')->get();
        return view('admin.login.index',['data'=>$data]);
    }

    public function postPhone (Request $request)
    {
        $phone = $request->input('mobile');
        $bool = DB::table('approve')
            -> where('phone',$phone)
            -> count();
        if($bool>0){
            return 0;
        }else{
            return 1;
        }
    }
    public function postUserphone (Request $request)
    {
        $phone = $request->input('mobile');
        $bool = DB::table('admin')
            -> where('phone',$phone)
            -> count();
        if($bool>0){
            return 0;
        }else{
            return 1;
        }
    }

    public function postAccount (Request $request) {
        $account = $request->input('user');
        $row = DB::table('approve') -> where('account',$account)->count();
        if($row>0){
            return 0;
        }else{
            return 1;
        }
    }

    public function getTest(){
        $email = new EmailController();
       $res = $email -> sendEmail(['user'=>['18595459410@163.com'],'title'=>'火灾报警'],['area'=>'nihao','num'=>'123456','content'=>'请及时处置']);
       dd($res);
    }
}
